package com.jd.org.service.impl;


import com.jd.basic.query.BaseQuery;
import com.jd.basic.service.impl.BaseServiceImpl;

import com.jd.basic.util.LoginContext;
import com.jd.basic.util.MD5Utils;
import com.jd.basic.util.PageList;
import com.jd.basic.util.StrUtils;

import com.jd.org.domain.Employee;
import com.jd.org.domain.Shop;
import com.jd.org.mapper.EmployeeMapper;

import com.jd.org.service.IEmployeeService;
import com.jd.sys.domain.Role;
import com.jd.sys.mapper.RoleMapper;

import com.jd.user.domain.Logininfo;
import com.jd.user.domain.User;
import com.jd.user.mapper.LogininfoMapper;
import com.jd.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper mapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Employee> findEmpByDeptId(Long id) {
        return mapper.findEmpByDeptId(id);
    }
    public void add(Employee employee){
        //盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 盐值 + 密码
        String pw = MD5Utils.encrypByMd5(salt + "1");
        // 员工默认密码
        employee.setSalt(salt);
        employee.setPassword(pw);
        // 保存数据 先 logininfo 再user
        // 构建logininfo对象
        Logininfo logininfo = new Logininfo();
        User user = new User();
        // 这个api 会将user对象中的字段值 复制到logininfo中 前提是两个实体字段相同
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setDisable(true);
        logininfo.setType(0);
        // 添加logininfo表
        System.err.println("Logininfo添加测试" + logininfo);
        logininfo.setSalt(salt);
        logininfo.setPassword(pw);
        logininfoMapper.add(logininfo);
        // 在添加user表 将logininfoid set到user表中
        employee.setLogininfoId(logininfo.getId());
        // 添加emp的时候 需要判断一下 是否有店铺id
        Shop shop = LoginContext.getUserInfo().getShop();
        // 不为null 则是店铺的人在新增员工
        if (null != shop){
            // 当前是店铺的人在新增员工
            employee.setShopId(shop.getId());
            super.add(employee);
            List<Role> roleList = roleMapper.findAll();
            Optional<Role> any = roleList.stream().filter(role -> "dz".equals(role.getSn())).findAny();
            any.ifPresent(role->{
                roleMapper.addMiddle(employee.getId(), Collections.singletonList(role.getId()));
            });
        }else {
            super.add(employee);
            // 平台的人在添加员工
            roleMapper.addMiddle(employee.getId(), employee.getRoles());
        }

    }
//    @Override
//    public PageList<Employee> queryByPage(BaseQuery query) {
//        PageList<Employee> pageList = super.queryByPage(query);
//        if (null == pageList || CollectionUtils.isEmpty(pageList.getRows())) {
//            return pageList;
//        }
//        // 将角色集合放入到每个人的对象下面
//        List<Employee> emps = pageList.getRows();
//
//        List<Long> empIds = emps.stream().map(Employee::getId).collect(Collectors.toList());
//        // 根据empIds集合获取到所有的角色
//        List<EmpAndRoleVo> empAndRoleList =  roleMapper.findByEmpIds(empIds);
//        // 将 empAndRoleList 转成 map key：empid  value：List<Long> roleId
//        Map<Long, List<Long>> map = empAndRoleList.stream().collect(Collectors.groupingBy(EmpAndRoleVo::getEmployeeId
//                , Collectors.mapping(EmpAndRoleVo::getRoleId, Collectors.toList())));
//        //遍历emps 将roleId集合放入到emp对象中
//        emps.forEach(emp->{
//            List<Long> roleIds = map.get(emp.getId());
//            emp.setRoles(roleIds);
//        });
//        return pageList;
//    }
    @Override
    public PageList<Employee> queryByPage(BaseQuery query) {

        Integer totals = mapper.queryByCount(query);
        System.err.println("查询的条数"+totals);
        if (0 >= totals){
            return new PageList<>(0,new ArrayList<>());
        }
        List<Employee> rows = mapper.queryByPage(query);
        return new PageList<>(totals,rows);
}
}
